<?php

namespace app\admin\controller;

use app\helper\Helper;
use app\model\ReRoleModel;
use think\Controller;
use think\Exception;
use think\Request;
use think\Session;

class Base extends Controller
{
    protected $service;

    protected $adminInfo;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->adminInfo['role_id'] = 1;
//        $this->checkAdmin();
    }

    /**
     * 检查是否登录
     */
    private function checkAdmin()
    {
        $this->adminInfo = Session::get('admin_info');
        if(empty($this->adminInfo)){
            $this->redirect('login/login');
        }
    }

    /**
     * 获取角色权限
     * @param $role_id
     * @return \think\Collection|\think\model\Collection
     * @throws \think\exception\DbException
     */
    protected function checkPermissions($role_id)
    {
        $ReRoleModel = new ReRoleModel();
        return $ReRoleModel->where('role_id',$role_id)->field('menu_id')->select();
    }


    /**
     * 筛选方法 （支持一维数组和二维数组）
     * @param $arr
     * @param $post
     * @return array
     */
    public function select_param($arr,$post)
    {
        if(!empty($arr)){
            $where = [];
            foreach ($arr as $key =>$value){
                if(is_array($value)){
                    $v = array_values($value);
                    $k = array_keys($value);
                    $a = explode('.',$v[0]);
                }else{
                    $a = explode('.',$value);
                }
                if (isset($post[$a[1]]) && $post[$a[1]]!=="") {
                    if($key == 'like'){
                        $where[$value] = array('like', '%'.$post[$a[1]].'%');
                    }elseif($key == 'array'){
                        $where[$v[0]] = [$k[0],$post[$a[1]]];
                    }else{
                        $where[$value] = $post[$a[1]];
                    }
                    $this->assign($a[1],$post[$a[1]]);
                }
            }
            return $where;
        }
    }

}